<template>
  <el-dialog title="新增员工" :visible="visible" @close="dialogClose" @open="dialogOpen">
    <!-- 表单 -->
    <el-form ref="addRef" :model="addForm" :rules="addRules" label-width="100px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="addForm.username" style="width:80%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="addForm.mobile" style="width:80%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="addForm.timeOfEntry" style="width:80%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="addForm.formOfEmployment" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in hireList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="addForm.workNumber" style="width:80%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-cascader
          v-model="addForm.departmentName"
          :options="options"
          :props="departProps"
          style="width:80%"
          :show-all-levels="false"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="addForm.correctionTime" style="width:80%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template #footer>
      <el-button @click="dialogClose">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartment, addEmployee } from '@/api'
import findSon from '@/utils/findSon'
import { Message } from 'element-ui'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      value: [],
      options: [],
      addForm: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      departProps: {
        label: 'name',
        value: 'name',
        checkStrictly: true
      },
      addRules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      },
      // 聘用形式
      hireList: [
        { value: 1, label: '正式' },
        { value: 2, label: '非正式' }
      ]
    }
  },
  methods: {
    // 弹窗关闭、清空表单、清空部门的值
    dialogClose() {
      this.$emit('dialog-close')
      this.$refs.addRef.resetFields()
      this.addForm.departmentName = ''
    },
    // 打开弹窗
    async dialogOpen() {
      const res = await getDepartment()
      this.options = findSon(res.data.depts)// 树形数据
    },
    // 点击确定提交
    // 进行表单的兜底校验
    // 引入接口、使用接口/发送请求
    // 提示用户
    // 关闭弹窗
    // 刷新列表
    submit() {
      this.$refs.addRef.validate(async valid => {
        if (!valid) return
        await addEmployee({
          ...this.addForm,
          departmentName: this.addForm.departmentName[this.addForm.departmentName.length - 1]
        })
        Message.success('用户添加成功')
        this.dialogClose()
        this.$emit('add')
      })
    }
  }
}
</script>

<style>

</style>
